<?php

namespace App\Repository;

use Hyperf\Context\Context;

class RepositoryContro
{
    private string $defaultDb = 'Msq';

    //数据来源
    const DATA_SOURCE = ['Redis' => true, 'Es' => true, 'Msq' => true];

    //缓存工具
    const DATA_SOURCE_CACHE = ['Redis'];


    private function getRepositoriesInstance(string $class): object
    {
        $classInstance = Context::get($class);
        if (empty($classInstance)) {
            $classInstance = new $class;
            Context::set($class, $classInstance);
        }
        return $classInstance;
    }

    public function __call($name, $arguments): mixed
    {
        $result = null;
        $sourceClass = [];
        foreach (self::DATA_SOURCE as $sourceName => $isUse) {
            $class = str_replace('Contro', $sourceName, static::class);
            if (!class_exists($class)) {
                continue;
            }
            if ($isUse && method_exists($class, $name)) {
                $sourceClass[$sourceName] = $this->getRepositoriesInstance($class);
                $result = $sourceClass[$sourceName]->$name(...$arguments);
                if ($sourceClass[$sourceName]->isContinue()) {
                    $sourceClass[$sourceName]->setContinue(false);
                    continue;
                }
                if (null !== $result) {
                    break;
                }
            }
        }
        return $result;
    }
}